<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="large" :inline="true">
      <el-form-item label="订单号" prop="username">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单流水号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择会员"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in userList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderState">
        <el-select
          v-model="queryParams.orderState"
          placeholder="请选择订单状态"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in orderState"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="large"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="large" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="18">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="large"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="large"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="large"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="large"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <el-table
      class="order-table"
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="订单编号" prop="id" width="100" align="center" />
      <el-table-column
        label="订单流水号"
        prop="orderSn"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
      />
      <el-table-column
        label="会员名"
        prop="memberName"
        width="100"
        align="center"
      />
      <el-table-column
        label="订单状态"
        prop="orderState"
        width="100"
        align="center"
        ><template slot-scope="{ row }">
          <el-tag type="primary">{{
            row.orderState == 1
              ? "待付款"
              : row.orderState == 2
              ? "待发货"
              : row.orderState == 3
              ? "待收货"
              : row.orderState == 4
              ? "待评价"
              : row.orderState == 5
              ? "已完成"
              : "已取消"
          }}</el-tag>
        </template></el-table-column
      >
      <el-table-column
        label="收货地址"
        prop="fullLocation"
        width="150"
        align="center"
      />
      <el-table-column
        label="收货人"
        prop="userName"
        width="100"
        align="center"
      />
      <el-table-column
        label="联系电话"
        prop="telNumber"
        width="100"
        align="center"
      />
      <el-table-column
        label="实际支付金额"
        prop="actualPrice"
        width="120"
        align="center"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        width="180"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.orderState === 2"
            size="large"
            type="text"
            icon="el-icon-edit"
            @click="handleSend(scope.row)"
            >发货</el-button
          >
          <el-button
            size="large"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="large"
            type="text"
            icon="el-icon-edit"
            @click="info(scope.row)"
            >详情</el-button
          >
          <el-button
            size="large"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row :span="24" style="margin-top: 20px">
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNo"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryParams.pageSize"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 添加或修改会员信息对话框 -->
    <el-dialog
      :title="(form.id === null ? '添加' : '修改') + '订单信息'"
      :visible.sync="open"
      width="500px"
      height="600px"
      lock-scroll
      top="5vh"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        size="mini"
      >
        <el-form-item label="订单流水号" prop="orderSn">
          <el-input v-model="form.orderSn" :disabled="true" />
        </el-form-item>
        <el-form-item label="会员" prop="userId">
           <el-select
            v-model="form.userId"
            placeholder="请选择会员"
            clearable
            style="width: 240px"
            :disabled="true"
          >
            <el-option
              v-for="(item, index) in userList"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderState">
          <el-select
            v-model="form.orderState"
            placeholder="请选择订单状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="(item, index) in orderState"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="快递公司" prop="expressCompany">
          <el-input
            v-model="form.expressCompany"
            placeholder="请输入快递公司"
          />
        </el-form-item>
        <el-form-item label="快递单号" prop="expressNumber">
          <el-input v-model="form.expressNumber" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="快递费用" prop="expressCost">
          <el-input v-model="form.expressCost" placeholder="请输入快递费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "orderList",
  data() {
    return {
      totalCount: 0,
      //搜索参数
      queryParams: {
        orderSn: "",
        userId: null,
        orderState: null,
        pageNo: 1,
        pageSize: 5,
      },
      //订单状态
      //1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消
      orderState: [
        { value: 1, label: "待付款" },
        { value: 2, label: "待发货" },
        { value: 3, label: "待收货" },
        { value: 4, label: "待评价" },
        { value: 5, label: "已完成" },
        { value: 6, label: "已取消" },
      ],
      userList: [],
      orderList: [],
      form: {
        id: null, // 订单编号
        orderSn: "", // 订单流水号
        userId: "", //微信用户Id；
        orderState: null, //1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消
        expressCompany: "", //快递公司
        expressNumber: "", //快递单号
        expressCost: "", //快递费用
      },
      rules: {
        orderState: [
          { required: true, message: "请选择订单状态", trigger: "change" },
        ],
        expressCompany: [
          { required: true, message: "请输入快递公司", trigger: "blur" },
        ],
        expressNumber: [
          { required: true, message: "请输入快递单号", trigger: "blur" },
        ],
        expressCost: [
          { required: true, message: "请输入快递费用", trigger: "blur" },
        ],
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      open: false, //是否打开添加或修改会员信息对话框
      loading: false,
      showSearch: true,
      ids: [], //选中的用户id集合
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    //获取用户列表
    async getList() {
      this.loading = true;
      try {
        const res = await this.$axios("/api/order/list", {
          params: this.queryParams,
        });
        if (res.data.code === 200) {
          this.totalCount = res.data.data.totalCount;
          this.orderList = res.data.data.data;
        }
      } catch (error) {
        this.$message.error("获取用户列表失败");
      } finally {
        this.loading = false;
      }
    },
    //查询所有会员
    async getUserList() {
      const res = await this.$axios("/api/wx/user/list");
      if (res.data.code === 200) {
        this.userList = res.data.data.data.map((item) => {
          return {
            label: item.username,
            value: item.id,
          };
        });
      }
    },
    //查询
    handleQuery() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.getList();
        } else {
          return false;
        }
      });
    },
    //重置查询条件
    resetQuery() {
      this.queryParams.orderState = null;
      this.queryParams.userId = null;
      this.queryParams.orderSn = "";
      this.getList();
    },
    //当页面变化时的事件
    handleCurrentChange(pageNo) {
      //修改当前页码
      this.queryParams.pageNo = pageNo;
      //执行查询
      this.handleQuery();
    },

    //每当页数量下拉框选项切换变化时的事件
    handleSizeChange(pageSize) {
      //修改查询条件中的的每页数量
      this.queryParams.pageSize = pageSize;
      //执行查询
      this.handleQuery();
    },
    
    handleSend(row) {
      this.$confirm(`确定发货吗?`, "提示", {
        type: "info",
      }).then(async () => {
        row.orderState = row.orderState + 1;
        const res = await this.$axios.post("/api/order/update", {
          orderState: row.orderState,
          id: row.id,
        });
        if (res.data.code === 200) {
          this.$message.success("发货成功");
          this.open = false;
          this.getList();
        } else {
          this.$message.error("发货异常");
        }
        this.getList();
      }) .catch(() => {
          this.$message.info("已取消发货");
        });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (this.form.id !== null) {
            //修改
            const res = await this.$axios.post("/api/order/update", this.form);
            if (res.data.code === 200) {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          } else {
            //添加
            const res = await this.$axios.post("/api/wx/user/add", this.form);
            if (res.data.code === 200) {
              this.$message.success("添加成功");
              this.open = false;
              this.getList();
            } else {
              this.$message.error(res.data.msg);
            }
          }
          this.form.avatar = "";
          this.$refs["form"].resetFields();
        }
      });
    },
    //取消按钮
    cancel() {
      this.open = false;
      this.$refs["form"].resetFields();
    },
    handleAdd() {
      (this.form = {
        id: null, // 订单编号
        orderSn: "", // 订单流水号
        userId: "", //微信用户Id；
        orderState: null, //1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消
        expressCompany: "", //快递公司
        expressNumber: "", //快递单号
        expressCost: "", //快递费用
      }),
        (this.open = true);
    },
    async handleUpdate(row) {
      const id = row.id || this.ids[0];
     const res = await this.$axios(`/api/order/detail?id=${id}`);
      if (res.data.code === 200) {
        this.form = res.data.data;
        this.open = true;
      } else {
        this.$message.error(res.data.msg);
      }
    },
    handleDelete(row) {
      const id = [row.id] || this.ids;
      this.$confirm(`是否删除订单编号为${id}的订单`, "提示", {
        type: "warning",
      })
        .then(async () => {
          const res = await this.$axios.post("/api/order/del", id);
          if (res.data.code === 200) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    handleExport() {
      // 导出逻辑
      this.$message.info("暂不支持导出功能");
    },
    //多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    info(row) {
      this.$router.push(`/orderInfo?id=${row.id}`);
    },
  },
};
</script>

<style scoped>
.order-table {
  margin-top: 20px;
}
.app-container {
  padding: 20px;
}
.el-form-item {
  margin-bottom: 20px;
}
.el-form-item label {
  font-size: 16px;
  font-weight: bold;
}
.avatar-uploader .el-upload {
  border: 1px dotted red !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
